<style type="text/css">
.sourcetb_out{
width:950px;
border:0px solid #CC3300;
overflow-x:scroll;
}
table.sourcetb{
border-collapse:collapse;
}
table.sourcetb th{
border: 1px solid #CCCCCC;
background-color: #E2F4C4;
padding: 5px;
text-align: center;
vertical-align: top;
}
table.sourcetb td{
border: 1px solid #CCCCCC;
background-color: #fcfcfc;
padding: 5px;
text-align: center;
vertical-align: top;
}

input.textBox,input.textBoxOff{
width:60px;
text-align:right;
}
input.textRemark{
width:200px;
text-align:left;
}
</style>
<script type="text/javascript">
var rs = {json_encode($rs)};
var rs_ku_vege = {json_encode($rs_ku_vege)};
var rs_ku_veges = {json_encode($rs_ku_veges)};
var rs_ku_index = {json_encode($rs_ku_index)};
var vegenums = {count($rs_ku_vege)};
var url_return = '/admin_ku/index';
$().ready(function(){
	showLoading();
	o2js.build('dates');
	o2js.build('ymprompt');
	o2js.build('jscalendar');
	 
//	Calendar.setup({
//		inputField:'plandate',
//		button:'slt_plandate',
//		ifFormat:"%Y-%m-%d",
//		showsTime:false
//	});
//	Calendar.setup({
//		inputField:'acdate',
//		button:'slt_acdate',
//		ifFormat:"%Y-%m-%d %H:%M:00",
//		showsTime:true
//	});
	fnGoCacl();
});


//获取DOM数值 重量
function fnGetZhong(v){
	if(isNull(v)){
		return 0;
	}
	if(!fnNum(v)){
		return 0;
	}
	return Math.abs(parseFloat(v));
}
//获取DOM数值 份数
function fnGetFen(v){
	if(isNull(v)){
		return 0;
	}
	if(!fnNum(v)){
		return 0;
	}
	return Math.abs(parseInt(v,10));
}
//计算之后的格式化
function fnSetZhong(v){
	return fnDec(v,3); 
}
//计算之后的格式化
function fnSetFen(v){
	return Math.floor(v); 
}
function fnGoCacl(){
	
	for(var i=1; i<=vegenums; i++){
		var cur_weight=0;
		var cur_nums=0;
		var cur_pack=0;
		var cg_price = fnGetZhong($('#cg_price_'+i).val());
		
		 
		//采购金额
		var  cg_total = fnGetZhong($('#cg_weight_'+i).val()) * cg_price;
		$('#cg_total_'+i).val( fnSetZhong(cg_total) );
		 
		//发货金额
		var  send_total = fnGetZhong($('#send_weight_'+i).val()) * cg_price;
		$('#send_total_'+i).val( fnSetZhong(send_total) );
		//云贵次品
		var yg_weight =  fnGetZhong($('#cg_weight_'+i).val()) - fnGetZhong($('#send_weight_'+i).val()) ;
		$('#yg_weight_'+i).val( fnSetZhong(yg_weight) );
		//次品金额
		var  yg_total = yg_weight * cg_price;
		$('#yg_total_'+i).val( fnSetZhong(yg_total) );
		
		//当前重量
		cur_weight = fnGetZhong($('#gcall_weight_'+i).val()) - fnGetZhong($('#gcloss_weight_'+i).val()) ;
	 
	  
	  //首次
		//库存原料
		$('#fst_kcyl_weight_'+i).val( fnSetZhong(cur_weight) );
		$('#fst_kcdz_nums_'+i).val( '0' );
		
		//原料损耗
		var fst_mateloss_weight = fnGetZhong($('#fst_mateloss_weight_'+i).val());
		cur_weight = cur_weight - fst_mateloss_weight;
		var fst_matemake_weight = fnGetZhong($('#fst_matemake_weight_'+i).val());
		cur_weight = cur_weight - fst_matemake_weight;
		
		var fst_packmake_nums = fnGetFen($('#fst_packmake_nums_'+i).val());
		cur_nums = fst_packmake_nums;
		var fst_pack = 0;
		if(fst_packmake_nums != 0){
			fst_pack = fnGetZhong( fst_matemake_weight/fst_packmake_nums );
		}
		//当前包装规格
		$('#fst_pack_'+i).val( fnSetZhong(fst_pack) );
		//平均包装规格
		cur_pack = fst_pack;
		//加工后
		$('#fst_jgh_weight_'+i).val( fnSetZhong(cur_weight) );
		$('#fst_jgh_nums_'+i).val( fnSetFen(cur_nums) );
		
		 
		//循环看添加的出库与加工
		var jia_indexs = 2;
		var chu_indexs = 1;
		for(var k=0; k<rs_ku_index.length ; k++){
			var obj = rs_ku_index[k];
			if(obj['type'] == 'chu'){
				var orderout_nums = fnGetFen($('#orderout_nums_'+i+'_'+chu_indexs).val());
				var actout_nums = fnGetFen($('#actout_nums_'+i+'_'+chu_indexs).val());
				
				//实际出库
				$('#actout_kg_'+i+'_'+chu_indexs).val( fnSetZhong(actout_nums*cur_pack) );
			 
				//当前库存
				cur_nums = cur_nums - actout_nums;
				
				$('#out_kcyl_'+i+'_'+chu_indexs).val( fnSetZhong(cur_weight) );
				$('#out_kcdz_'+i+'_'+chu_indexs).val( fnSetFen(cur_nums) ); 
				 
				chu_indexs++;
			}else{
				
				$('#kcyl_weight_'+i+'_'+jia_indexs).val( fnSetZhong(cur_weight) );
				$('#kcdz_nums_'+i+'_'+jia_indexs).val( fnSetFen(cur_nums) );
				
				//袋装损耗
				var packloss_nums = fnGetFen($('#packloss_nums_'+i+'_'+jia_indexs).val());
				cur_nums = cur_nums - packloss_nums;
				//原料损耗
				var mateloss_weight = fnGetZhong($('#mateloss_weight_'+i+'_'+jia_indexs).val());
				cur_weight = cur_weight - mateloss_weight;
				//加工使用
				var matemake_weight = fnGetZhong($('#matemake_weight_'+i+'_'+jia_indexs).val());
				cur_weight = cur_weight - matemake_weight;
				//生产袋装
				var packmake_nums = fnGetFen($('#packmake_nums_'+i+'_'+jia_indexs).val());
				cur_nums = cur_nums + packmake_nums;
				//包装规格
				var pack = 0;
				if(packmake_nums != 0){
					pack = fnGetZhong( matemake_weight/packmake_nums );
				}
				$('#pack_'+i+'_'+jia_indexs).val( fnSetZhong(pack) );
				//算平均包装规格
				if(pack > 0){
					cur_pack = fnGetZhong( (cur_pack+pack)/2 );
				}
				//加工后
				$('#jgh_weight_'+i+'_'+jia_indexs).val( fnSetZhong(cur_weight) );
				$('#jgh_nums_'+i+'_'+jia_indexs).val( fnSetFen(cur_nums) );
				
				jia_indexs++;
			}
		}
		//成品报废
		var bf_weight = fnGetZhong($('#bf_weight_'+i).val());
		cur_weight = cur_weight - bf_weight;
		//实际剩余
		$('#end_weight_'+i).val( fnSetZhong(cur_weight) );
		$('#end_nums_'+i).val( fnSetFen(cur_nums) );
	}
	setTimeout(fnGoCacl,500);
}



function fnNewChu(id){
	fnConfirm({
		str:"确认添加一次订单与出库？",
		func_true:function(e){
			fnLoading('正在操作');
			$('button').attr('disabled','disabled');
			$.post(
				'/admin_ku/plan_add',
				{
					ku_id:rs.id,
					id:id
				},
				function(e){
					fnLoadingEnd();
					$('button').attr('disabled','');
					var ok = errorHandler(e);
					if(ok && ok.success=="1"){　 
						window.location.reload();

					}
				}
			);
		}
	});
}
function fnNewJia(id){
	fnConfirm({
		str:"确认添加一次加工？",
		func_true:function(e){
			fnLoading('正在操作');
			$('button').attr('disabled','disabled');
			$.post(
				'/admin_ku/make_add',
				{
					ku_id:rs.id,
					id:id
				},
				function(e){
					fnLoadingEnd();
					$('button').attr('disabled','');
					var ok = errorHandler(e);
					if(ok && ok.success=="1"){　 
						window.location.reload();
					}
				}
			);
		}
	});
}
function fnDelChu(id,i){
	fnConfirm({
		str:"确认删除第 "+i+" 次订单与出库？",
		func_true:function(e){
			fnLoading('正在删除');
			$('button').attr('disabled','disabled');
			$.post(
				'/admin_ku/plan_del',
				{
					id:id
				},
				function(e){
					fnLoadingEnd();
					$('button').attr('disabled','');
					var ok = errorHandler(e);
					if(ok && ok.success=="1"){　 
						window.location.reload();
					}
				}
			);
		}
	});
}
function fnDelJia(id,i){
	fnConfirm({
		str:"确认删除第 "+i+" 次加工？",
		func_true:function(e){
			fnLoading('正在删除');
			$('button').attr('disabled','disabled');
			$.post(
				'/admin_ku/make_del',
				{
					id:id
				},
				function(e){
					fnLoadingEnd();
					$('button').attr('disabled','');
					var ok = errorHandler(e);
					if(ok && ok.success=="1"){　 
						window.location.reload();
					}
				}
			);
		}
	});
}


/* 自动输入实际出库 */
function fnAutoAct(n){
	for(var i=1; i<=vegenums ; i++){
		$('#actout_nums_'+i+'_'+n).val( $('#orderout_nums_'+i+'_'+n).val() );
	}
} 
/* 自动输入计划订单 */
function fnAutoOrder(n){ 
	ymPrompt.close();
	ymPrompt.win({
		title:'选择配送日期',
		fixPosition:true,
		maxBtn:true,
		width: 550, 
		height: 350,
		iframe:{
			src:'/admin_ku/plan_ordernum_sltdate?n='+n+'&endfunc=fnAutoOrderEnd'
		}
	});
} 
function fnAutoOrderEnd(e){
	var n = e.n;
	$.post(
		'/admin_ku/plan_ordernum',
		{
			senddate:e.senddate,
			sendpi:e.sendpi,
			orderpi:e.orderpi
		},
		function(e){
			fnLoadingEnd();
			$('button').attr('disabled','');
			var ok = errorHandler(e);
			if(ok && ok.success=="1"){　 
				for(var i=1; i<=vegenums ; i++){
					if(ok.results[$('#tr_'+i).attr('vege_id')]){
						$('#orderout_nums_'+i+'_'+n).val( ok.results[$('#tr_'+i).attr('vege_id')] );
					}else{
						$('#orderout_nums_'+i+'_'+n).val('0');
					}
				}
			}
		}
	);
}
function fnReturn(){
	jumpTo(url_return);
}
</script>